//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DiscountingBondEngine.h"
using namespace Cephei::QL::Pricingengines::Bond;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::CDiscountingBondEngine (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeSettlementDateFlows) : CPricingEngine(CDiscountingBondEngine::typeid)
{
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phDiscountingBondEngine = NULL;
#endif
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountCurve))
        {
            _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve->Value);
            _CdiscountCurve->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _discountCurve = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (discountCurve) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        boost::optional<bool> _includeSettlementDateFlows;
        if (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeSettlementDateFlows))
        {
            boost::optional<bool> t = boost::optional<bool> (ValueHelper::Convert (includeSettlementDateFlows->Value));
            _includeSettlementDateFlows = boost::optional<boost::optional<bool>> (t);
        }
        else
            _includeSettlementDateFlows = boost::none;
        _ppDiscountingBondEngine = new boost::shared_ptr<QuantLib::DiscountingBondEngine> (new QuantLib::DiscountingBondEngine ( _discountCurve,  _includeSettlementDateFlows ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppDiscountingBondEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::CDiscountingBondEngine (boost::shared_ptr<QuantLib::DiscountingBondEngine>& childNative, Object^ owner) : CPricingEngine(CDiscountingBondEngine::typeid)
{
#ifdef HANDLE
	_phDiscountingBondEngine = NULL;
#endif
	_ppDiscountingBondEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppDiscountingBondEngine));
}
Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::CDiscountingBondEngine (QuantLib::DiscountingBondEngine& childNative, Object^ owner) : CPricingEngine(CDiscountingBondEngine::typeid)
{
#ifdef HANDLE
	_phDiscountingBondEngine = NULL;
#endif
	_ppDiscountingBondEngine = new boost::shared_ptr<QuantLib::DiscountingBondEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppDiscountingBondEngine));
    _DiscountingBondEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::CDiscountingBondEngine (CDiscountingBondEngine^ copy) : CPricingEngine(CDiscountingBondEngine::typeid)
{
#ifdef HANDLE
	_phDiscountingBondEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscountingBondEngine = new boost::shared_ptr<QuantLib::DiscountingBondEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppDiscountingBondEngine));
    }
}
Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::CDiscountingBondEngine (System::Type^ t) : CPricingEngine(CDiscountingBondEngine::typeid)
{
#ifdef HANDLE
	_phDiscountingBondEngine = NULL;
#endif
	if (!t->IsSubclassOf(CDiscountingBondEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::CDiscountingBondEngine (QuantLib::Handle<QuantLib::DiscountingBondEngine>& childNative, Object^ owner)  : CPricingEngine(CDiscountingBondEngine::typeid)
{
	_phDiscountingBondEngine = &childNative;
	_ppDiscountingBondEngine = &static_cast<boost::shared_ptr<QuantLib::DiscountingBondEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppDiscountingBondEngine));
    _DiscountingBondEngineOwner = owner;
}
Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::CDiscountingBondEngine (QuantLib::Handle<QuantLib::DiscountingBondEngine> childNative)  : CPricingEngine(CDiscountingBondEngine::typeid)
{
	_phDiscountingBondEngine = &childNative;
	_ppDiscountingBondEngine = &static_cast<boost::shared_ptr<QuantLib::DiscountingBondEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppDiscountingBondEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::CDiscountingBondEngine (QuantLib::DiscountingBondEngine childNative)  : CPricingEngine(CDiscountingBondEngine::typeid)
{
#ifdef HANDLE
	_phDiscountingBondEngine = NULL;
#endif
	_ppDiscountingBondEngine = new boost::shared_ptr<QuantLib::DiscountingBondEngine> (new QuantLib::DiscountingBondEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppDiscountingBondEngine));
}
#endif

Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::~CDiscountingBondEngine ()
{
    if (_ppDiscountingBondEngine != NULL)
    {
	    delete _ppDiscountingBondEngine;
        _ppDiscountingBondEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::!CDiscountingBondEngine ()
{
    if (_ppDiscountingBondEngine != NULL)
    {
	    delete _ppDiscountingBondEngine;
    }
}
QuantLib::DiscountingBondEngine& Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::GetReference ()
{
    if (_ppDiscountingBondEngine == NULL) throw gcnew NativeNullException ();
	return **_ppDiscountingBondEngine;
}
boost::shared_ptr<QuantLib::DiscountingBondEngine>& Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::GetShared ()
{
    if (_ppDiscountingBondEngine == NULL) throw gcnew NativeNullException ();
	return *_ppDiscountingBondEngine;
}
QuantLib::DiscountingBondEngine* Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::GetPointer ()
{
    if (_ppDiscountingBondEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppDiscountingBondEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscountingBondEngine>& Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::GetHandle ()
{
	if (_phDiscountingBondEngine == NULL)
	{
		_phDiscountingBondEngine = new Handle<QuantLib::DiscountingBondEngine> (*_ppDiscountingBondEngine);
	}
	return *_phDiscountingBondEngine;
}
#endif
bool Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::HasNative () 
{
	return (_ppDiscountingBondEngine != NULL);
}

Cephei::QL::Pricingengines::Bond::IDiscountingBondEngine^ Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::Calculate::get ()
{
    try
    {
    	(*_ppDiscountingBondEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine::DiscountCurve::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure> _rv = (Handle<QuantLib::YieldTermStructure>)(*_ppDiscountingBondEngine)->discountCurve ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::CYieldTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Bond::IDiscountingBondEngine^ Cephei::QL::Pricingengines::Bond::CDiscountingBondEngine_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeSettlementDateFlows)
{
    return gcnew CDiscountingBondEngine ( discountCurve,  includeSettlementDateFlows);
}
